import { Request, Response } from 'express';
import { getRepository } from 'typeorm';
import { StudyGoal } from '../models/StudyGoal';
import { AuthRequest } from '../middlewares/auth';

// 获取学习目标列表
export const getStudyGoals = async (req: AuthRequest, res: Response) => {
  try {
    const studyGoalRepository = getRepository(StudyGoal);
    const studyGoals = await studyGoalRepository.find({
      where: { user: { id: req.user.id } }
    });

    res.json(studyGoals);
  } catch (error) {
    res.status(500).json({ message: '获取学习目标失败', error: error.message });
  }
};

// 获取单个学习目标
export const getStudyGoal = async (req: AuthRequest, res: Response) => {
  try {
    const { id } = req.params;
    const studyGoalRepository = getRepository(StudyGoal);
    const studyGoal = await studyGoalRepository.findOne({
      where: { id: Number(id), user: { id: req.user.id } }
    });

    if (!studyGoal) {
      return res.status(404).json({ message: '学习目标不存在' });
    }

    res.json(studyGoal);
  } catch (error) {
    res.status(500).json({ message: '获取学习目标失败', error: error.message });
  }
};

// 创建学习目标
export const createStudyGoal = async (req: AuthRequest, res: Response) => {
  try {
    const { targetWordCount, targetCategoryCount, targetDate } = req.body;
    const studyGoalRepository = getRepository(StudyGoal);

    const newStudyGoal = studyGoalRepository.create({
      user: { id: req.user.id },
      targetWordCount,
      targetCategoryCount,
      targetDate,
      isCompleted: false,
      currentProgress: 0
    });

    await studyGoalRepository.save(newStudyGoal);
    res.status(201).json(newStudyGoal);
  } catch (error) {
    res.status(500).json({ message: '创建学习目标失败', error: error.message });
  }
};

// 更新学习目标
export const updateStudyGoal = async (req: AuthRequest, res: Response) => {
  try {
    const { id } = req.params;
    const { targetWordCount, targetCategoryCount, targetDate, isCompleted, currentProgress } = req.body;
    const studyGoalRepository = getRepository(StudyGoal);

    const studyGoal = await studyGoalRepository.findOne({
      where: { id: Number(id), user: { id: req.user.id } }
    });

    if (!studyGoal) {
      return res.status(404).json({ message: '学习目标不存在' });
    }

    Object.assign(studyGoal, {
      targetWordCount,
      targetCategoryCount,
      targetDate,
      isCompleted,
      currentProgress
    });

    await studyGoalRepository.save(studyGoal);
    res.json(studyGoal);
  } catch (error) {
    res.status(500).json({ message: '更新学习目标失败', error: error.message });
  }
};

// 删除学习目标
export const deleteStudyGoal = async (req: AuthRequest, res: Response) => {
  try {
    const { id } = req.params;
    const studyGoalRepository = getRepository(StudyGoal);
    const studyGoal = await studyGoalRepository.findOne({
      where: { id: Number(id), user: { id: req.user.id } }
    });

    if (!studyGoal) {
      return res.status(404).json({ message: '学习目标不存在' });
    }

    await studyGoalRepository.remove(studyGoal);
    res.json({ message: '删除成功' });
  } catch (error) {
    res.status(500).json({ message: '删除学习目标失败', error: error.message });
  }
}; 